//
//  GestureConfigViewController.m
//  cxd4iphone
//
//  Created by hexy on 12/25/15.
//  Copyright © 2015 hexy. All rights reserved.
//

#import "GestureConfigViewController.h"
#import "GestureViewController.h"
#import "GestureVerifyViewController.h"
#import "GestureManager.h"

#define kReuseId @"xxx"
@interface GestureConfigViewController () <UIAlertViewDelegate>

@property(nonatomic, assign) int sectionCount;

@end

@implementation GestureConfigViewController

- (instancetype)initWithStyle:(UITableViewStyle)style {
    
    return [super initWithStyle: UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.sectionCount = 1;

    self.view.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1.0];
    
    self.tableView.separatorColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1.0];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier: kReuseId];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.sectionCount;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuseId forIndexPath:indexPath];
    
    if (indexPath.section == 0) {
        
        UISwitch *s = [[UISwitch alloc] init];
        s.on = [GestureManager userSettedGesture];
        [s addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
        cell.accessoryView = s;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = @"手势密码";
    }
    
    if (indexPath.section == 1) {
        
        cell.textLabel.text = @"修改手势密码";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        
        GestureVerifyViewController *gestureVerifyVc = [[GestureVerifyViewController alloc] init];
        gestureVerifyVc.isToSetNewGesture = YES;
        [self.navigationController pushViewController:gestureVerifyVc animated:YES];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

- (void)switchChanged:(UISwitch *)s {
    
    if (s.isOn) {
        
        UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"只有在需要进行登录操作的时候需要手势密码" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        [al show];
    } else {
        
        GestureVerifyViewController *gestureVerifyVc = [[GestureVerifyViewController alloc] init];
        [self.navigationController pushViewController:gestureVerifyVc animated:YES];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    GestureViewController *gestureVc = [[GestureViewController alloc] init];
    gestureVc.type = GestureViewControllerTypeSetting;
    [self.navigationController pushViewController:gestureVc animated:YES];

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if ([GestureManager userSettedGesture]) {
        
        self.sectionCount = 2;
        
    } else {
        
        self.sectionCount = 1;
    }
    [self.tableView reloadData];
}

@end
